package fr.afcepf.al22.qualite.dao;

import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

import javax.script.SimpleScriptContext;
import javax.sql.DataSource;

import com.mysql.jdbc.Statement;

import fr.afcepf.al22.qualite.data.util.Al22DataSource;
import fr.afcepf.al22.qualite.entitee.Personne;

/**
 * 
 * @author Stagiaire
 *
 */
public class DaoPersonneImpl implements IDaoPersonne {
	DataSource ds = new Al22DataSource() {
		
		@Override
		public <T> T unwrap(Class<T> paramIface) throws SQLException {
			// TODO Auto-generated method stub
			return null;
		}
		
		@Override
		public boolean isWrapperFor(Class<?> paramIface) throws SQLException {
			// TODO Auto-generated method stub
			return false;
		}
		
		@Override
		public void setLoginTimeout(int paramSeconds) throws SQLException {
			// TODO Auto-generated method stub
			
		}
		
		@Override
		public void setLogWriter(PrintWriter paramOut) throws SQLException {
			// TODO Auto-generated method stub
			
		}
		
		@Override
		public Logger getParentLogger() throws SQLFeatureNotSupportedException {
			// TODO Auto-generated method stub
			return null;
		}
		
		@Override
		public int getLoginTimeout() throws SQLException {
			// TODO Auto-generated method stub
			return 0;
		}
		
		@Override
		public PrintWriter getLogWriter() throws SQLException {
			// TODO Auto-generated method stub
			return null;
		}
	};
	/**
	 * 
	 */
	private String regAjout = "INSERT INTO Personne VALUES (null,?,?,?,?)";
	/**
	 * 
	 */
	private final int indiceNomAjout = 1;
	/**
	 * 
	 */
	private final int indiceMailAjout = 2;
	/**
	 * 
	 */
	private final int indiceMdpAjout = 3;
	/**
	 * 
	 */
	private final int indiceNaissanceAjout = 4;
	private final int indiceIdGenere = 1;
	@Override
	public Personne ajouter(Personne paramPers) {
		Connection cnx = null;
		try {
			cnx = ds.getConnection();
			PreparedStatement pstmt = cnx.prepareStatement(regAjout,Statement.RETURN_GENERATED_KEYS);
			
			pstmt.setString(indiceNomAjout, paramPers.getNom());
			pstmt.setString(indiceMailAjout, paramPers.getMail());
			pstmt.setString(indiceMdpAjout, paramPers.getMdp());
			pstmt.setDate(indiceNaissanceAjout, new Date(paramPers.getNaissance().getTime()));
			int nbLigne = pstmt.executeUpdate();
			ResultSet rsKey = pstmt.getGeneratedKeys();
			if (nbLigne ==1 && rsKey.next()) {
				paramPers.setId(rsKey.getInt(indiceIdGenere));	
			}

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				cnx.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return paramPers;
	}

	
	private Personne rsToPersonne(ResultSet rs){
		Personne p = null;
		try {
			p = new Personne(rs.getInt("id"),rs.getString("nom"), rs.getString("mail"),
					rs.getString("mdp"),rs.getDate("naissance")
			);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return p;
	}
	Connection cnx = null;
	@Override
	public Personne verifMail(String paramMail) {
		Personne p = null;
		
		try {
			cnx = ds.getConnection();
			PreparedStatement pstmt = cnx.prepareStatement("SELECT p.* FROM Personne p WHERE p.mail =?");
			pstmt.setString(1, paramMail);
			ResultSet rs = pstmt.executeQuery();
			if (rs.next()) {
				p = rsToPersonne(rs);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return p;
		
	}
	public List<Personne> recherche(String paramNom) {
		
		List<Personne> listeP = new ArrayList<Personne>();
		try {
			cnx = ds.getConnection();
			PreparedStatement pstmt;
			pstmt = cnx.prepareStatement("SELECT p.* FROM Personne p WHERE p.nom =?");
			pstmt.setString(1, paramNom);
			ResultSet rs = pstmt.executeQuery();
			
			while (rs.next()) {
				listeP.add(rsToPersonne(rs));
				
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return listeP;
		
	}

}
